package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.TbParkingUseMapper;
import com.hqyj.pojo.TbParkingUse;
import com.hqyj.service.TbParkingService;
import com.hqyj.service.TbParkingUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 车位使用表 服务实现类
 * </p>
 *
 * @author gp
 * @since 2021-09-26
 */
@Service
public class TbParkingUseServiceImpl implements TbParkingUseService {

    @Autowired
    private TbParkingUseMapper tbParkingUseMapper;

    @Autowired
    private TbParkingService tbParkingService;

    @Override
    public HashMap<String, Object> listParkUse(TbParkingUse tbParkingUse) {
        HashMap<String, Object> map = new HashMap<String,Object>();
        //第一步:创建分页对象
        Page<TbParkingUse> tbParkingUsePage = new Page<>(tbParkingUse.getPage(), tbParkingUse.getRow());
        //创建条件查询对象
        QueryWrapper<TbParkingUse> queryWrapper = new QueryWrapper<>();
        //第二步：查询分页，获取分页数据集
        Page<TbParkingUse> pageList = tbParkingUseMapper.selectPage(tbParkingUsePage, queryWrapper);
        //第三步：
        // 获取当前页的条数，返回值是一个list
        map.put("info",pageList.getRecords());
        //获取总条数
        map.put("total",pageList.getTotal());
        //获取总页数
        map.put("totalPage",pageList.getPages());

        return map;
    }

    @Override
    public HashMap<String, Object> insertParkUse(TbParkingUse tbParkingUse) {
        HashMap<String,Object> map  = new HashMap<>();
        int num = tbParkingUseMapper.insert(tbParkingUse);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存失败");
        }
        return map;
    }


    @Transactional
    @Override
    public HashMap<String, Object> buyParkUse(TbParkingUse tbParkingUse) {
        HashMap<String,Object> map  = new HashMap<>();

        //计算出购买车位的总费用（一年5000）
        int totalFee = Integer.parseInt(tbParkingUse.getUseType())*5000;
        tbParkingUse.setTotalFee(totalFee);

        //通过传入的购买时间+购买年限，得到到期的时间
        long createTime = tbParkingUse.getCreateTime().toEpochSecond(ZoneOffset.of("+8"));
        //一年的秒数
        long oneyear=31536000L;
        long temp =createTime+oneyear*Integer.parseInt(tbParkingUse.getUseType());
        LocalDateTime updateTime =LocalDateTime.ofEpochSecond(temp,0, ZoneOffset.ofHours(8));

        //修改TbParking表中被购买的车位的信息
        int num1 = tbParkingService.updateNameAndUpdateTime(tbParkingUse.getCode(), "私有车位", updateTime);
        //业主购买车位后，新增一条数据到TbParkingUse表中
        tbParkingUse.setUpdateTime(updateTime);
        int num2 = tbParkingUseMapper.insert(tbParkingUse);
        if(num1+num2==2){
            map.put("info","保存成功");
        }else{
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    @Transactional
    public HashMap<String, Object> startPark(TbParkingUse tbParkingUse) {
        HashMap<String,Object> map  = new HashMap<>();
        //1.通过前端传来的业主姓名和电话号码确定用户是否购买停车位
        TbParkingUse tbParkingUse1 = tbParkingUseMapper.selectByTelAndOwnerName(tbParkingUse.getTelephone(), tbParkingUse.getOwnerName());
        //2.如果购买了停车位，就修改停车位使用表的信息，加入车牌号码，以及开始停车时间
        if(tbParkingUse1 != null){
           tbParkingUseMapper.updateMessage(tbParkingUse.getCarNumber(),tbParkingUse1.getId(),LocalDateTime.now());
            map.put("info","ok");
        }else{
            //3.如果没有购买停车位，就新增一条数据，加入车牌号码，以及开始停车时间
            TbParkingUse one = new TbParkingUse();
            one.setStartTime(LocalDateTime.now());
            one.setCarNumber(tbParkingUse.getCarNumber());
            tbParkingUseMapper.insert(one);
            map.put("info","ok");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> endPark(TbParkingUse tbParkingUse) {
        HashMap<String,Object> map  = new HashMap<>();
        //1.通过前端传来的业主姓名和电话号码确定用户是否购买停车位
        TbParkingUse tbParkingUse1 = tbParkingUseMapper.selectByTelAndOwnerName(tbParkingUse.getTelephone(), tbParkingUse.getOwnerName());
        //2.如果购买了停车位，就修改停车位使用表的信息，结束停车时间
        if(tbParkingUse1 != null){
            tbParkingUseMapper.updateEndTime(tbParkingUse1.getId(),LocalDateTime.now());
            map.put("info","ok1"); // 代表买了车位的信息
        }else{
            //3.如果没有购买停车位，就新增一条数据，加入车牌号码，以及结束停车时间
//            TbParkingUse one = new TbParkingUse();
//            one.setEndTime(LocalDateTime.now());
//            one.setCarNumber(tbParkingUse.getCarNumber());
//            //查询出数据库后台没有购买停车位和停车了但未出车的数据
//            List<TbParkingUse> list = tbParkingUseMapper.selectByCarNumber(one.getCarNumber());
//            Iterator<TbParkingUse> iterator = list.iterator();
//            while(iterator.hasNext()){
//                TbParkingUse next = iterator.next();
//                if(next.getEndTime() == null){
//                    tbParkingUseMapper.updateEndTimeById(next.getId(),one.getEndTime());
//                }
//            }
            map.put("info","ok2"); // 代表未买买了车位的信息
        }
        return map;
    }

    @Override
    public HashMap<String, Object> endParkingFee(TbParkingUse tbParkingUse) {
        HashMap<String,Object> map  = new HashMap<>();
        //3.如果没有购买停车位，就新增一条数据，加入车牌号码，以及结束停车时间
        TbParkingUse one = new TbParkingUse();
        one.setEndTime(LocalDateTime.now());
        one.setCarNumber(tbParkingUse.getCarNumber());
        //查询出数据库后台没有购买停车位和停车了但未出车的数据
        List<TbParkingUse> list = tbParkingUseMapper.selectByCarNumber(one.getCarNumber());
        Iterator<TbParkingUse> iterator = list.iterator();
        while(iterator.hasNext()){
            TbParkingUse next = iterator.next();
            if(next.getEndTime() == null){
                int totalFee;
                Long temp = 0L;
                temp = one.getEndTime().toEpochSecond(ZoneOffset.of("+8")) - next.getStartTime().toEpochSecond(ZoneOffset.of("+8"));
                //将时间戳转化成时间
                LocalDateTime dateTime =LocalDateTime.ofEpochSecond(temp,0, ZoneOffset.ofHours(0));
                if(dateTime.getSecond()>0){
                    totalFee=dateTime.getHour()+1;
                }else{
                    totalFee=dateTime.getHour();
                }
                tbParkingUseMapper.updateEndTimeAndTotalFeeById(next.getId(),one.getEndTime(),totalFee);
                map.put("info","保存成功");
            }
        }
        return map;
    }
}
